#!/bin/sh
# PCP QA Test No. 1640
# pmUnregisterDerived() workout
#
# non-valgrind variant, see qa/1641 for the valgrind variant
#
# Copyright (c) 2025 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    [ "$PCPQA_VALGRIND" = both ] || \
        _notrun "valgrind variant qa/1641 will be run"
fi

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \
	-e '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/[^ ]*/TIMESTAMP/' \
	-e "s/`hostname`/HOSTNAME/g" \
    # end
}

_doit()
{
    if $do_valgrind
    then
	_run_valgrind src/unregister "$@"
    else
	src/unregister "$@" 2>&1
    fi \
    | _filter
}

cat <<End-of-File >$tmp.cases
-- unregister unknown metric
-u foobar foobar
--- definition and unregister, but no context
-r foo=sample.bin -u foo foo
--- value 42 for global derived metric
-r foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: foo.bar
--- global derived metric unregistered
-r foobar=(8*sample.long.ten+4*sample.long.one)/2 -h local: -u foobar foobar
--- value 42 for per-context derived metric in 2nd context only
-h `hostname` -h local: -q foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: foo.bar
--- per-context drived metric unregistered
-h `hostname` -h local: -q foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: -u foo.bar foo.bar
--- value 42 in 2 contexts
-r foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: -h `hostname` foo.bar
--- name undefined in all contexts
-r foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: -h `hostname` -u foo.bar foo.bar
--- value 42 in the 1st context, name undefined in the 2nd context
-h local: -q foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h `hostname` foo.bar
-- global and per-context derived metrics unregistered
-r foo.bar=(8*sample.long.ten+4*sample.long.one)/2 -h local: -h `hostname` -q bar.foo=(8*sample.long.ten+4*sample.long.one)/2 -u foo.bar -u bar.foo foo.bar bar.foo
--- load
-h local: -c $tmp.config foo.load
--- load and unregister
-c $tmp.config -h local: -u foo.load foo.load
--- masking
-c $tmp.config -h local: -q foo.load=sample.long.million foo.load
--- masking and unregister
-c $tmp.config -h local: -q foo.load=sample.long.million -u foo.load foo.load
End-of-File

cat <<End-of-File >$tmp.config
foo.load=(8*sample.long.ten+4*sample.long.one)/2
End-of-File

# real QA test starts here
cat $tmp.cases | while read args
do
    case "$args"
    in
	---*)
		echo "$args ---"
		;;
	*)
		echo "+ $args" | _filter
		_doit $args
		echo
		;;
    esac
done

# success, all done
exit
